// pages/dailiangfang/surroundings.js
Page({
  data: {
    scrollviewheight: '',
    ci_rwdid: '',
    WorksID: '',
    CustomerID: '',
    CatalogID: '',
    baseimageurl: 'http://img0.wenes.cn/Upload/Project/',
    // 图册数组
    array1: [], // 公司logo	116
    array2: [], // VI形象	117
    array3: [], // 企业形象	118
    array4: [], // 企业图腾	119
    array5: [], // 企业色彩	120
    array6: [], // 办公室环境	121
  },
  
  onLoad: function (options) {
    var that = this
    that.setData({
      ci_rwdid: options.ci_rwdid,
      WorksID: options.WorksID,
      CustomerID: options.CustomerID,
    }) 
  },

  /**
    * 生命周期函数--监听页面显示
    */
  onShow: function () {
    console.log('onShow')
    var that = this
    that.requestPic(that.data.ci_rwdid)
  },

  /**
   * 获取照片urls
   */
  requestPic: function (ci_rwdid) {
    var that = this
    // 图册数组
    var array1 = []
    var array2 = []
    var array3 = []
    var array4 = []
    var array5 = []
    var array6 = []
    wx.request({
      url: 'https://api.wenes.cn/JD/GetAllImages?ci_rwdid=' + that.data.ci_rwdid,
      success: function (res) {
        var length = res.data.Table.length
        console.log(res.data)
        for (var i = 0; i < length; i++) {
          if (res.data.Table[i].CatalogID == '116') {
            array1.push(res.data.Table[i].ImageUrl)
            that.setData({
              array1: array1
            })
          }
          if (res.data.Table[i].CatalogID == '117') {
            array2.push(res.data.Table[i].ImageUrl)
            that.setData({
              array2: array2
            })
          }
          if (res.data.Table[i].CatalogID == '118') {
            array3.push(res.data.Table[i].ImageUrl)
            that.setData({
              array3: array3
            })
          }

          if (res.data.Table[i].CatalogID == '119') {
            array4.push(res.data.Table[i].ImageUrl)
            that.setData({
              array4: array4
            })
          }
          if (res.data.Table[i].CatalogID == '120') {
            array5.push(res.data.Table[i].ImageUrl)
            that.setData({
              array5: array5
            })
          }
          if (res.data.Table[i].CatalogID == '121') {
            array6.push(res.data.Table[i].ImageUrl)
            that.setData({
              array6: array6
            })
          }
        }
      }
    })
  },

  // 点击相册
  uploadimage116: function (e) {
    var that = this
    if (that.data.array1.length > 0) {
      wx.navigateTo({
        url: 'album?ci_rwdid=' + that.data.ci_rwdid + '&CatalogID=116&WorksID=' + that.data.WorksID + '&CustomerID=' + that.data.CustomerID
      })
    }
    if (that.data.array1 == 0) {
      that.uploadPic('116')
    }
  },
  uploadimage117: function (e) {
    var that = this
    if (that.data.array2.length > 0) {
      wx.navigateTo({
        url: 'album?ci_rwdid=' + that.data.ci_rwdid + '&CatalogID=117&WorksID=' + that.data.WorksID + '&CustomerID=' + that.data.CustomerID
      })
    }
    if (that.data.array2 == 0) {
      that.uploadPic('117')
    }
  },
  uploadimage118: function (e) {
    var that = this
    if (that.data.array3.length > 0) {
      wx.navigateTo({
        url: 'album?ci_rwdid=' + that.data.ci_rwdid + '&CatalogID=118&WorksID=' + that.data.WorksID + '&CustomerID=' + that.data.CustomerID
      })
    }
    if (that.data.array3 == 0) {
      that.uploadPic('118')
    }
  },
  uploadimage119: function (e) {
    var that = this
    if (that.data.array4.length > 0) {
      wx.navigateTo({
        url: 'album?ci_rwdid=' + that.data.ci_rwdid + '&CatalogID=119&WorksID=' + that.data.WorksID + '&CustomerID=' + that.data.CustomerID
      })
    }
    if (that.data.array4 == 0) {
      that.uploadPic('119')
    }
  },
  uploadimage120: function (e) {
    var that = this
    if (that.data.array5.length > 0) {
      wx.navigateTo({
        url: 'album?ci_rwdid=' + that.data.ci_rwdid + '&CatalogID=120&WorksID=' + that.data.WorksID + '&CustomerID=' + that.data.CustomerID
      })
    }
    if (that.data.array5 == 0) {
      that.uploadPic('120')
    }
  },
  uploadimage121: function (e) {
    var that = this
    if (that.data.array6.length > 0) {
      wx.navigateTo({
        url: 'album?ci_rwdid=' + that.data.ci_rwdid + '&CatalogID=121&WorksID=' + that.data.WorksID + '&CustomerID=' + that.data.CustomerID
      })
    }
    if (that.data.array6 == 0) {
      that.uploadPic('121')
    }
  },
  // 本地选择图片
  uploadPic: function (CatalogID) {
    var that = this
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        // 上传图片
        wx.uploadFile({
          url: 'https://api.wenes.cn/jd/UploadJDimages',
          filePath: tempFilePaths[0],
          name: 'Filedata',
          formData: {
            'qt': 'upload',
            'type': '1',
            'W': '500',
            'H': '300',
            'HW': 'HW',
            'WorksID': that.data.WorksID,
            'CatalogID': CatalogID,
            'UserId': that.data.CustomerID,
            // 'WorksID': 8749,
            // 'CatalogID': 125,
            // 'UserId': 2015,
          },
          success: function (res) {
            if (res.statusCode == 200) {
              wx.showToast({
                title: '成功',
                icon: 'success',
                duration: 2000
              })
              // 实时刷新
              that.requestPic(that.data.ci_rwdid) 
            } else {
              wx.showToast({
                title: '失败',
                icon: 'failure',
                duration: 2000
              })
            }
          }
        })
      },
    })
  },

  // 页面跳转
  measureroom: function () {
    var that = this
    wx.redirectTo({
      url: 'measureroom?ci_rwdid=' + that.data.ci_rwdid,
    })
  },
  demand: function () {
    var that = this
    wx.redirectTo({
      url: 'demand?ci_rwdid=' + that.data.ci_rwdid,
    })
  },
  upkeep: function () {
    var that = this
    wx.redirectTo({
      url: 'upkeep?ci_rwdid=' + that.data.ci_rwdid,
    })
  },


})